import { Model, DataTypes, Optional } from 'sequelize';
import { sequelize } from '../database';

// 评论属性接口
export interface CommentAttributes {
  id: string;
  content: string;
  status: 'pending' | 'approved' | 'rejected';
  article_id: string;
  user_id: string | null;
  parent_id: string | null;
  author_name: string | null;
  author_email: string | null;
  author_ip: string | null;
  user_agent: string | null;
  referrer: string | null;
  reviewed_by: string | null;
  reviewed_at: Date | null;
  reject_reason: string | null;
  like_count: number;
  dislike_count: number;
  reply_count: number;
  is_pinned: boolean;
  is_hidden: boolean;
  created_at: Date;
  updated_at: Date;
  deleted_at: Date | null;
}

// 创建时可选的属性
interface CommentCreationAttributes extends Optional<CommentAttributes, 
  'id' | 'status' | 'parent_id' | 'user_id' | 'author_name' | 'author_email' | 
  'author_ip' | 'user_agent' | 'referrer' | 'reviewed_by' | 'reviewed_at' | 
  'reject_reason' | 'like_count' | 'dislike_count' | 'reply_count' | 'is_pinned' | 
  'is_hidden' | 'created_at' | 'updated_at' | 'deleted_at'> {}

// 评论模型类
class Comment extends Model<CommentAttributes, CommentCreationAttributes> implements CommentAttributes {
  public id!: string;
  public content!: string;
  public status!: 'pending' | 'approved' | 'rejected';
  public article_id!: string;
  public user_id!: string | null;
  public parent_id!: string | null;
  public author_name!: string | null;
  public author_email!: string | null;
  public author_ip!: string | null;
  public user_agent!: string | null;
  public referrer!: string | null;
  public reviewed_by!: string | null;
  public reviewed_at!: Date | null;
  public reject_reason!: string | null;
  public like_count!: number;
  public dislike_count!: number;
  public reply_count!: number;
  public is_pinned!: boolean;
  public is_hidden!: boolean;
  
  // 时间戳
  public readonly created_at!: Date;
  public readonly updated_at!: Date;
  public readonly deleted_at!: Date | null;

  // 关联
  public readonly article?: any; // Article模型
  public readonly user?: any; // User模型
  public readonly parent?: Comment;
  public readonly replies?: Comment[];
  public readonly reviewer?: any; // User模型
}

// 初始化模型
Comment.init(
  {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
    },
    content: {
      type: DataTypes.TEXT,
      allowNull: false,
      validate: {
        notEmpty: true,
      },
    },
    status: {
      type: DataTypes.ENUM('pending', 'approved', 'rejected'),
      allowNull: false,
      defaultValue: 'pending',
    },
    article_id: {
      type: DataTypes.UUID,
      allowNull: false,
      references: {
        model: 'articles',
        key: 'id',
      },
    },
    user_id: {
      type: DataTypes.UUID,
      allowNull: true,
      references: {
        model: 'users',
        key: 'id',
      },
    },
    parent_id: {
      type: DataTypes.UUID,
      allowNull: true,
      references: {
        model: 'comments',
        key: 'id',
      },
    },
    author_name: {
      type: DataTypes.STRING(100),
      allowNull: true,
    },
    author_email: {
      type: DataTypes.STRING(100),
      allowNull: true,
      validate: {
        isEmail: true,
      },
    },
    author_ip: {
      type: DataTypes.STRING(50),
      allowNull: true,
    },
    user_agent: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
    referrer: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
    reviewed_by: {
      type: DataTypes.UUID,
      allowNull: true,
      references: {
        model: 'users',
        key: 'id',
      },
    },
    reviewed_at: {
      type: DataTypes.DATE,
      allowNull: true,
    },
    reject_reason: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
    like_count: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
    },
    dislike_count: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
    },
    reply_count: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
    },
    is_pinned: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
    },
    is_hidden: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
    deleted_at: {
      type: DataTypes.DATE,
      allowNull: true,
    },
  },
  {
    sequelize,
    modelName: 'comment',
    tableName: 'comments',
    timestamps: true,
    paranoid: true, // 软删除
    underscored: true, // 使用下划线命名法
    hooks: {
      afterCreate: async (comment: Comment) => {
        // 如果是回复评论，更新父评论的回复数
        if (comment.parent_id) {
          await Comment.increment('reply_count', {
            where: { id: comment.parent_id },
          });
        }
      },
    },
  }
);

// 自关联：评论回复关系
Comment.hasMany(Comment, {
  as: 'replies',
  foreignKey: 'parent_id',
  onDelete: 'CASCADE',
});

Comment.belongsTo(Comment, {
  as: 'parent',
  foreignKey: 'parent_id',
});

export default Comment; 